package org.hyd.controller;

import org.hyd.common.JsonResult;
import org.hyd.model.Student;
import org.hyd.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class StudentController {
//    private static Logger logger= LoggerFactory.getLogger(StudentController.class);
    @Autowired
    StudentService studentService;

    //学生登录
    @RequestMapping(value = "/studentlogin")
    @ResponseBody
    public Object studentLogin(String studentId, String password, String code, HttpServletRequest httpServletRequest){
        JsonResult<Student> jsonResult;
        try {
            //从session中获取验证码
            String mycode=httpServletRequest.getSession().getAttribute("code").toString();
            if (mycode.equals(code)){
                Student student =studentService.studentLogin(studentId,password);
                if (student!=null){
                    jsonResult=new JsonResult<Student>("200","查询成功",student);
                }else {
                    jsonResult=new JsonResult<Student>("404","用户不在或者密码不对",student);
                }
            }else {
                jsonResult=new JsonResult<Student>("404","验证码不对");
            }
        }catch (Exception ex){
            ex.printStackTrace();
//            logger.error(ex.getMessage());
            jsonResult=new JsonResult<Student>("500","查询失败");
        }
        return jsonResult;
    }

    //根据学号查询
    @RequestMapping(value = "/selectstudentbyid")
    @ResponseBody
    public Object SelectStudentById(String studentId){
        JsonResult<Student> jsonResult;
        try {
            Student student =studentService.selectStudentById(studentId);
            jsonResult=new JsonResult<Student>("200","查询成功",student);
        }catch (Exception ex){
            ex.printStackTrace();
//            logger.error(ex.getMessage());
            jsonResult=new JsonResult<Student>("500","查询失败");
        }
        return jsonResult;
    }
}
